package com.azt.front.interceptor;

import com.azt.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FrontContextInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	private UserService userService;

//	private static final String SSOLOGIN = "http://www.azt.com:8090/user/signin";
//	public static final String KEY_SESSION = "x_sess";
	
//	@Value("${redirecturl}")
//	private String SSOLOGIN;
	
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
		/*String requestURI = request.getRequestURI();
		*//**
		 * 格式化请求路径
		 *//*
		requestURI = requestURI.replaceAll("/{2,}", "/");
		
		if (requestURI.startsWith("/azt")) {
			//获取cookie
			Cookie cookie = CookieUtils.getCookieByName(request, KEY_SESSION);
			
			// cookie 为 null 时 跳到登录页  
			if(cookie == null){
				sendToLogin(request, response);
				return false;
			}
		}*/

		return true;
	}


//
//	private void sendToLogin(HttpServletRequest request, HttpServletResponse response) {
//		String URL = getURL(request);
//		try {
//			response.sendRedirect(URL);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//	
//	
//	private String getURL(HttpServletRequest request) {
//		String params = request.getQueryString();
//		String URL = SSOLOGIN + "?redirect=" + (params != null ? request.getRequestURL().append("?").append(params).toString() : request.getRequestURL().toString());
//		return URL;
//	}

}